package newtank.network.client.handler;

import lombok.AllArgsConstructor;
import newtank.TankClientFrame;
import newtank.data.ShootSignal;
import newtank.utils.ExecutorsUtils;
import newtank.view.BulletView;
import newtank.view.PaintView;
import newtank.view.TankView;
import newtank.view.context.BulletViewContext;
import server.netty.GpcDataPackage;

/**
 * @author lijiahao
 * @since 2022/3/18
 */
@AllArgsConstructor
public class ShootDataHandler implements DataHandler<ShootSignal> {

    private final TankClientFrame tankClientFrame;

    @Override
    public ShootSignal decodePackage(GpcDataPackage gpcDataPackage) {
        return new ShootSignal(gpcDataPackage.getUnique());
    }

    @Override
    public void handle(GpcDataPackage gpcDataPackage) {
        ShootSignal shootSignal = decodePackage(gpcDataPackage);
        String unique = shootSignal.getUnique();
        PaintView paintView = tankClientFrame.getView(unique);
        BulletViewContext bulletViewContext = new BulletViewContext();
        if(paintView instanceof TankView) {
            ((TankView) paintView).shooting(bulletViewContext);
            tankClientFrame.getDrawingCloth().addPaintView(new BulletView(bulletViewContext));
            ExecutorsUtils.execute(bulletViewContext);
        }
    }

    @Override
    public boolean support(GpcDataPackage gpcDataPackage) {
        return false;
    }
}
